# -*-* encoding:UTF-8 -*-
# author            : mengy
# date              : 2018/12/7
# python-version    : Python 3.7.0
# description       : 解析城市名称和代号
#                     12306城市列表查询地址:
#                           https://kyfw.12306.cn/otn/resources/js/framework/station_name.js?station_version=1.9077

import re
import requests
import urllib
from urllib import request
from pprint import pprint

url = "https://kyfw.12306.cn/otn/resources/js/framework/station_name.js?station_version=1.9077"
## 方法一
# 解决warning:
#       InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised.
# requests.packages.urllib3.disable_warnings()
# response = requests.get(url, verify=False)
# stations = re.findall(u'([\u4e00-\u9fa5]+)\|([A-Z]+)', response.text)
# pprint(dict(stations), indent=4)

## 方法二
request = urllib.request.Request(url)
response = urllib.request.urlopen(request).read().decode('UTF-8')

stations = re.findall(r'([\u4e00-\u9fa5]+)\|([A-Z]+)', response)        #匹配中文和对应的英文
stations = dict(stations)
stations = dict(zip(stations.keys(), stations.values()))                #将匹配的内容转化为字典
pprint(stations,indent=4)                                                        #以列的形式打印出来
